﻿using System;

namespace UniRx.Operators
{
	// Token: 0x020003F9 RID: 1017
	internal class ThrottleFirstFrameObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x060014C5 RID: 5317 RVA: 0x0005DC42 File Offset: 0x0005C042
		public ThrottleFirstFrameObservable(IObservable<T> source, int frameCount, FrameCountType frameCountType) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.frameCount = frameCount;
			this.frameCountType = frameCountType;
		}

		// Token: 0x060014C6 RID: 5318 RVA: 0x0005DC65 File Offset: 0x0005C065
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			return new ThrottleFirstFrameObservable<T>.ThrottleFirstFrame(this, observer, cancel).Run();
		}

		// Token: 0x04000C11 RID: 3089
		private readonly IObservable<T> source;

		// Token: 0x04000C12 RID: 3090
		private readonly int frameCount;

		// Token: 0x04000C13 RID: 3091
		private readonly FrameCountType frameCountType;

		// Token: 0x020003FA RID: 1018
		private class ThrottleFirstFrame : OperatorObserverBase<T, T>
		{
			// Token: 0x060014C7 RID: 5319 RVA: 0x0005DC74 File Offset: 0x0005C074
			public ThrottleFirstFrame(ThrottleFirstFrameObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x060014C8 RID: 5320 RVA: 0x0005DC98 File Offset: 0x0005C098
			public IDisposable Run()
			{
				this.tick = new ThrottleFirstFrameObservable<T>.ThrottleFirstFrame.ThrottleFirstFrameTick(this);
				this.cancelable = new SerialDisposable();
				IDisposable disposable = this.parent.source.Subscribe(this);
				return StableCompositeDisposable.Create(this.cancelable, disposable);
			}

			// Token: 0x060014C9 RID: 5321 RVA: 0x0005DCDC File Offset: 0x0005C0DC
			private void OnNext()
			{
				object obj = this.gate;
				lock (obj)
				{
					this.open = true;
				}
			}

			// Token: 0x060014CA RID: 5322 RVA: 0x0005DD1C File Offset: 0x0005C11C
			public override void OnNext(T value)
			{
				object obj = this.gate;
				lock (obj)
				{
					if (!this.open)
					{
						return;
					}
					this.observer.OnNext(value);
					this.open = false;
				}
				SingleAssignmentDisposable singleAssignmentDisposable = new SingleAssignmentDisposable();
				this.cancelable.Disposable = singleAssignmentDisposable;
				singleAssignmentDisposable.Disposable = Observable.TimerFrame(this.parent.frameCount, this.parent.frameCountType).Subscribe(this.tick);
			}

			// Token: 0x060014CB RID: 5323 RVA: 0x0005DDB8 File Offset: 0x0005C1B8
			public override void OnError(Exception error)
			{
				this.cancelable.Dispose();
				object obj = this.gate;
				lock (obj)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x060014CC RID: 5324 RVA: 0x0005DE20 File Offset: 0x0005C220
			public override void OnCompleted()
			{
				this.cancelable.Dispose();
				object obj = this.gate;
				lock (obj)
				{
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
				}
			}

			// Token: 0x04000C14 RID: 3092
			private readonly ThrottleFirstFrameObservable<T> parent;

			// Token: 0x04000C15 RID: 3093
			private readonly object gate = new object();

			// Token: 0x04000C16 RID: 3094
			private bool open = true;

			// Token: 0x04000C17 RID: 3095
			private SerialDisposable cancelable;

			// Token: 0x04000C18 RID: 3096
			private ThrottleFirstFrameObservable<T>.ThrottleFirstFrame.ThrottleFirstFrameTick tick;

			// Token: 0x020003FB RID: 1019
			private class ThrottleFirstFrameTick : IObserver<long>
			{
				// Token: 0x060014CD RID: 5325 RVA: 0x0005DE88 File Offset: 0x0005C288
				public ThrottleFirstFrameTick(ThrottleFirstFrameObservable<T>.ThrottleFirstFrame parent)
				{
					this.parent = parent;
				}

				// Token: 0x060014CE RID: 5326 RVA: 0x0005DE97 File Offset: 0x0005C297
				public void OnCompleted()
				{
				}

				// Token: 0x060014CF RID: 5327 RVA: 0x0005DE99 File Offset: 0x0005C299
				public void OnError(Exception error)
				{
				}

				// Token: 0x060014D0 RID: 5328 RVA: 0x0005DE9C File Offset: 0x0005C29C
				public void OnNext(long _)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.open = true;
					}
				}

				// Token: 0x04000C19 RID: 3097
				private readonly ThrottleFirstFrameObservable<T>.ThrottleFirstFrame parent;
			}
		}
	}
}
